# -*- shell-script -*-

#  Copyright (C) 2015 Curt Brune <curt@cumulusnetworks.com>
#  Copyright (C) 2015-2016 david_yang <david_yang@accton.com>
#
#  SPDX-License-Identifier:     GPL-2.0

# Common block device definitions for the DEMO OS

demo_mnt="/demo"

# Configure UEFI / Boot Loader to load ONIE GRUB at the next boot.
# This is a one-shot event, reverting to the regular boot after one
# iteration.
set_onie_next_boot()
{
    if [ ! -d "$demo_mnt/grub" ] ; then
        # Do nothing if no GRUB installed in demo partition
        return 0
    fi
    if [ -d "/sys/firmware/efi/efivars" ] ; then
        # For the UEFI case we set the UEFI variable "BootNext" to
        # select ONIE as the next OS to boot.  The "BootNext" variable
        # is only used once by the firmware, after which the permanent
        # "BootOrder" variable is used.
        local boot_num=$(efibootmgr | grep "ONIE:" | awk '{ print $1 }')
        boot_num=${boot_num#Boot}
        # Remove trailing '*'
        boot_num=${boot_num%\*}
        if [ -n "$boot_num" ] ; then
            efibootmgr --quiet --bootnext $boot_num && return
            echo "ERROR: Problems setting UEFI BootNext variable"
        fi
    fi

    # Fall back to using GRUB chainload method
    grub-reboot --boot-directory="$demo_mnt" ONIE
}
